package com.example.app.components.MyTextField

import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Clear
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldColors
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.example.app.components.MyButton.MyIconButton
import com.example.app.components.MyIcon.MyIcon
import com.example.app.components.MyText.MyText
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update

class MyTextFieldState {
    var _textFieldValue = MutableStateFlow<TextFieldValue>(TextFieldValue(""))
    var textFieldValue = _textFieldValue.asStateFlow()
    var _isError = MutableStateFlow<Boolean>(false)
    var isError = _isError.asStateFlow()
    var _errorMessage = MutableStateFlow<String>("请输入必填字段")
    var errorMessage = _errorMessage.asStateFlow()
    var _isDirty = MutableStateFlow<Boolean>(false)
    var isDirty = _isDirty.asStateFlow()
    var _focus = MutableStateFlow<Boolean>(false)
    var focus = _focus.asStateFlow()
    var _focusCount = MutableStateFlow<Int>(0)
    var focusCount = _focusCount.asStateFlow()
    fun flowUpdate_textFieldValue(value: TextFieldValue) {
        _textFieldValue.update {
            value
        }
    }

    fun flowUpdate_isError(value: Boolean) {
        _isError.update {
            value
        }
    }

    fun flowUpdate_errorMessage(value: String) {
        _errorMessage.update {
            value
        }
    }

    fun flowUpdate_focus(value: Boolean) {
        _focus.update {
            value
        }
    }

    fun flowUpdate_isDirty(value: Boolean) {
        _isDirty.update {
            value
        }
    }

    fun flowUpdate_focusCount() {
        var count = _focusCount.value + 1
        _focusCount.update {
            count
        }
        if (count > 1) {
            this.flowUpdate_isDirty(true)
        }
    }
}
@Composable
fun MyTextField(
    myTextFieldState: MyTextFieldState,
    value: TextFieldValue,
    onValueChange: (TextFieldValue) -> Unit,
    shadow: Modifier? = null,
    modifier: Modifier = Modifier
        .fillMaxWidth()
        .onFocusChanged {
            myTextFieldState.flowUpdate_focus(it.isFocused)
            if (!it.isFocused) {
                myTextFieldState.flowUpdate_focusCount()
            }
        }
        .then(if (shadow != null) shadow else Modifier),
    enabled: Boolean = true,
    readOnly: Boolean = false,
    textStyle: TextStyle = LocalTextStyle.current,
    label: @Composable (() -> Unit)? = null,
    placeholder: @Composable (() -> Unit)? = {
        MyText(text = "请输入")
    },
    leadingIcon: @Composable (() -> Unit)? = null,
    trailingIcon: @Composable (() -> Unit)? = {
        var textFieldValue = myTextFieldState.textFieldValue.collectAsState()
        if (!textFieldValue.value.text.isEmpty()) {
            MyIconButton(onClick = { myTextFieldState.flowUpdate_textFieldValue(TextFieldValue("")) }) {
                MyIcon(imageVector = Icons.Rounded.Clear)
            }
        } else {
        }
    },
    prefix: @Composable (() -> Unit)? = null,
    suffix: @Composable (() -> Unit)? = null,
    supportingText: @Composable (() -> Unit)? = {
        var isError = myTextFieldState.isError.collectAsState()
        var errorMessage = myTextFieldState.errorMessage.collectAsState()
        if (isError.value) {
            Row(modifier = Modifier.fillMaxWidth()) {
                Text(
                    text = errorMessage.value,
                    modifier = Modifier.fillMaxWidth(),
                    color = MaterialTheme.colorScheme.error
                )
            }
        }
    },
    isError: Boolean = false,
    visualTransformation: VisualTransformation = VisualTransformation.None,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
    keyboardActions: KeyboardActions = KeyboardActions.Default,
    singleLine: Boolean = true,
    maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE,
    minLines: Int = 1,
    interactionSource: MutableInteractionSource? = null,
    shape: Shape = OutlinedTextFieldDefaults.shape,
    colors: TextFieldColors = OutlinedTextFieldDefaults.colors(
        errorContainerColor = MaterialTheme.colorScheme.surface,
        focusedContainerColor = MaterialTheme.colorScheme.surface,
        unfocusedContainerColor = MaterialTheme.colorScheme.surface,
        unfocusedBorderColor = Color.Transparent,
        focusedPlaceholderColor = OutlinedTextFieldDefaults.colors().focusedPlaceholderColor.copy(
            .3f
        ),
        unfocusedLabelColor = OutlinedTextFieldDefaults.colors().focusedPlaceholderColor.copy(
            .3f
        ),
        focusedBorderColor = Color.Transparent
    )
) {
    OutlinedTextField(
        value,
        onValueChange,
        modifier,
        enabled,
        readOnly,
        textStyle,
        label,
        placeholder,
        leadingIcon,
        trailingIcon,
        prefix,
        suffix,
        supportingText,
        isError,
        visualTransformation,
        keyboardOptions,
        keyboardActions,
        singleLine,
        maxLines,
        minLines,
        interactionSource,
        shape,
        colors,
    )
}